@cc_on /* 2> nul
@SETLOCAL
@ECHO OFF

REM -- check command-line usage
IF NOT "%~1"=="" GOTO UsageOk
REM
ECHO Usage: elevate (command-line)
ECHO E.g.:  elevate del c:\foo
ECHO.
ECHO Executes the command line under UAC elevation, if elevation appears to be
ECHO required.
ECHO.
ECHO Notes:
ECHO   -- This batch file attempts to write a temporary file to the root of the
ECHO      system drive; if that fails, it assumes UAC elevation is required.
ECHO   -- The message "A required privilege is not held by the client" indicates
ECHO      that you're running in a non-UAC-elevated state.
ECHO   -- The batch file exit code is that of the command line.
ECHO   -- Input redirection (using "^<^") is not supported.
ECHO   -- Output redirection (using "^>^") occurs outside this batch file.  For
ECHO      example, "elevate echo foo ^> bar" doesn't execute "echo foo ^> bar"
ECHO      under elevation -- it echoes "foo" under elevation and writes the result
ECHO      to "bar" outside of elevation.
REM
EXIT /B 1
:UsageOk

REM -- set %TestFile% to the name of a new test file we'll try to create in the root of the system
REM -- drive; if we succeed, we'll assume we're elevated (or UAC isn't enabled)
:NameTestFile
SET TestFile=%SystemDrive%\_Test%RANDOM%.tmp
IF EXIST "%TestFile%" GOTO NameTestFile

REM -- try to create %TestFile%
ECHO > "%TestFile%" 2> nul
IF EXIST "%TestFile%" GOTO UacDisabled
GOTO UacEnabled

:UacDisabled

REM -- feedback
ECHO Elevation not required ----------- %TestFile%

REM -- delete the test file we created
DEL "%TestFile%"

REM -- execute (command-line) and exit with its exit code
CALL %*
EXIT /B %ERRORLEVEL%

:UacEnabled

REM -- create a batch file (which will contain (command-line)), a signal file (which will be
REM -- created when the batch file completes, to signal completion to this script and provide its
REM -- exit code), and an output file (to contain the output of the batch file)
:NameBaseFile
SET BaseFile=%TEMP%\_Elevate%RANDOM%
IF EXIST "%BaseFile%.bat" GOTO NameBaseFile
IF EXIST "%BaseFile%.sig.bat" GOTO NameBaseFile
IF EXIST "%BaseFile%.out" GOTO NameBaseFile
ECHO CD /D "%CD%" > "%BaseFile%.bat"
ECHO CALL %* ^> "%BaseFile%.out" 2^>^&1 >> "%BaseFile%.bat"
ECHO ECHO SET ExitCode=^%%ERRORLEVEL^%% ^> "%BaseFile%.sig.bat" >> "%BaseFile%.bat"
IF NOT EXIST "%BaseFile%.bat" (
    ECHO Cannot create "%BaseFile%.bat"
    EXIT /B 1
)

REM -- execute the JScript part of this script to request elevation and run the batch file
CScript /nologo /e:JScript "%~f0" "%BaseFile%.bat" "%BaseFile%.sig.bat"

REM -- display output from the (command-line)
TYPE "%BaseFile%.out"

REM -- execute the signal file as a batch file, to set %ExitCode% to the exit code of
REM (command-line)
CALL "%BaseFile%.sig.bat"

REM -- clean up
DEL "%BaseFile%.bat" 2> nul
DEL "%BaseFile%.sig.bat" 2> nul
DEL "%BaseFile%.out" 2> nul

REM -- return with the exit code from (command-line), retrieved from the signal file
EXIT /B %ExitCode%

*/

// this is the JScript part of the script:

// get JScript command-line arguments
var batchFilePath = WScript.Arguments(0);
var signalFilePath = WScript.Arguments(1);

// request elevation and execute the batch file
var shell = new ActiveXObject("Shell.Application");
var obj = shell.ShellExecute("cmd.exe", "/c \"" + batchFilePath + "\"", "", "runas");

// wait for the signal file to be created
var fs = new ActiveXObject("Scripting.FileSystemObject");
while (!fs.FileExists(signalFilePath))
    WScript.Sleep(100);

